package com.muyu.danmo.utils;

import cn.hutool.json.JSONUtil;
import cn.jiguang.sdk.api.PushApi;
import cn.jiguang.sdk.bean.push.PushSendParam;
import cn.jiguang.sdk.bean.push.PushSendResult;
import cn.jiguang.sdk.bean.push.audience.Audience;
import cn.jiguang.sdk.bean.push.message.notification.NotificationMessage;
import cn.jiguang.sdk.enums.platform.Platform;
import com.muyu.system.domain.enums.JpushMessageEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;


import java.util.*;

/**
 * @author Administrator
 */
@Configuration
@RequiredArgsConstructor
@Slf4j
public class JPushUtils {


    private final PushApi pushApi;


    /**
     * 基础推送
     * @param id
     * @param jpushMessage
     */
    public void send(Long id, JpushMessageEnum jpushMessage) {
        PushSendParam param = new PushSendParam();
        // 发送给单个人
        Audience audience = new Audience();
        audience.setAliasList(Collections.singletonList(id.toString()));
        param.setAudience(audience);
        // 指定平台
        param.setPlatform(List.of(Platform.android,Platform.ios));
        // 通知内容
        NotificationMessage.Android android = new NotificationMessage.Android();
        android.setAlert(jpushMessage.getAlert());
        android.setTitle(jpushMessage.getTitle());
        Map<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("url",jpushMessage.getUrl());
        android.setExtras(objectObjectHashMap);

        NotificationMessage.IOS iOS = new NotificationMessage.IOS();
        iOS.setAlert(jpushMessage.getAlert());
        iOS.setExtras(objectObjectHashMap);


        NotificationMessage notificationMessage = new NotificationMessage();
        notificationMessage.setAndroid(android);
        notificationMessage.setIos(iOS);
        param.setNotification(notificationMessage);
        // 发送
        log.info("param:{}", JSONUtil.toJsonStr(param));
        PushSendResult result = null;
        try {
            result = pushApi.send(param);
            log.info("result:{}", JSONUtil.toJsonStr(result));
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
    }


}